home *** CD-ROM | disk | FTP | other *** search
/ Sky at Night 2007 June / SAN CD 6-2007 CD-ROM 25.iso / pc / Software / AstroGrav_Win / Java / jre1.6.0 / lib / rt.jar / java / net / PlainSocketImpl.class (.txt) < prev    next >
Encoding:
Java Class File  |  2006-11-29  |  7.7 KB  |  513 lines

  1. package java.net;
  2.  
  3. import java.io.FileDescriptor;
  4. import java.io.IOException;
  5. import java.io.InputStream;
  6. import java.io.OutputStream;
  7. import java.security.AccessController;
  8. import sun.net.ConnectionResetException;
  9. import sun.security.action.LoadLibraryAction;
  10.  
  11. class PlainSocketImpl extends SocketImpl {
  12.    int timeout;
  13.    private int trafficClass;
  14.    private boolean shut_rd = false;
  15.    private boolean shut_wr = false;
  16.    private SocketInputStream socketInputStream = null;
  17.    private int fdUseCount = 0;
  18.    private Object fdLock = new Object();
  19.    private boolean closePending = false;
  20.    private int CONNECTION_NOT_RESET = 0;
  21.    private int CONNECTION_RESET_PENDING = 1;
  22.    private int CONNECTION_RESET = 2;
  23.    private int resetState;
  24.    private Object resetLock = new Object();
  25.    private FileDescriptor fd1;
  26.    private InetAddress anyLocalBoundAddr = null;
  27.    private int lastfd = -1;
  28.    public static final int SHUT_RD = 0;
  29.    public static final int SHUT_WR = 1;
  30.  
  31.    PlainSocketImpl() {
  32.    }
  33.  
  34.    PlainSocketImpl(FileDescriptor var1) {
  35.       this.fd = var1;
  36.    }
  37.  
  38.    protected synchronized void create(boolean var1) throws IOException {
  39.       this.fd = new FileDescriptor();
  40.       this.fd1 = new FileDescriptor();
  41.       this.socketCreate(var1);
  42.       if (this.socket != null) {
  43.          this.socket.setCreated();
  44.       }
  45.  
  46.       if (this.serverSocket != null) {
  47.          this.serverSocket.setCreated();
  48.       }
  49.  
  50.    }
  51.  
  52.    protected void connect(String var1, int var2) throws UnknownHostException, IOException {
  53.       Object var3 = null;
  54.  
  55.       try {
  56.          InetAddress var4 = InetAddress.getByName(var1);
  57.  
  58.          try {
  59.             this.connectToAddress(var4, var2, this.timeout);
  60.             return;
  61.          } catch (IOException var6) {
  62.             var3 = var6;
  63.          }
  64.       } catch (UnknownHostException var7) {
  65.          var3 = var7;
  66.       }
  67.  
  68.       this.close();
  69.       throw var3;
  70.    }
  71.  
  72.    protected void connect(InetAddress var1, int var2) throws IOException {
  73.       this.port = var2;
  74.       this.address = var1;
  75.  
  76.       try {
  77.          this.connectToAddress(var1, var2, this.timeout);
  78.       } catch (IOException var4) {
  79.          this.close();
  80.          throw var4;
  81.       }
  82.    }
  83.  
  84.    protected void connect(SocketAddress var1, int var2) throws IOException {
  85.       if (var1 != null && var1 instanceof InetSocketAddress) {
  86.          InetSocketAddress var3 = (InetSocketAddress)var1;
  87.          if (var3.isUnresolved()) {
  88.             throw new UnknownHostException(var3.getHostName());
  89.          } else {
  90.             this.port = var3.getPort();
  91.             this.address = var3.getAddress();
  92.  
  93.             try {
  94.                this.connectToAddress(this.address, this.port, var2);
  95.             } catch (IOException var5) {
  96.                this.close();
  97.                throw var5;
  98.             }
  99.          }
  100.       } else {
  101.          throw new IllegalArgumentException("unsupported address type");
  102.       }
  103.    }
  104.  
  105.    private void connectToAddress(InetAddress var1, int var2, int var3) throws IOException {
  106.       if (var1.isAnyLocalAddress()) {
  107.          this.doConnect(InetAddress.getLocalHost(), var2, var3);
  108.       } else {
  109.          this.doConnect(var1, var2, var3);
  110.       }
  111.  
  112.    }
  113.  
  114.    public void setOption(int var1, Object var2) throws SocketException {
  115.       if (this.isClosedOrPending()) {
  116.          throw new SocketException("Socket Closed");
  117.       } else {
  118.          boolean var3 = true;
  119.          switch (var1) {
  120.             case 1:
  121.                if (var2 == null || !(var2 instanceof Boolean)) {
  122.                   throw new SocketException("bad parameter for TCP_NODELAY");
  123.                }
  124.  
  125.                var3 = (Boolean)var2;
  126.                break;
  127.             case 3:
  128.                if (var2 == null || !(var2 instanceof Integer)) {
  129.                   throw new SocketException("bad argument for IP_TOS");
  130.                }
  131.  
  132.                this.trafficClass = (Integer)var2;
  133.                break;
  134.             case 4:
  135.                if (var2 == null || !(var2 instanceof Boolean)) {
  136.                   throw new SocketException("bad parameter for SO_REUSEADDR");
  137.                }
  138.  
  139.                var3 = (Boolean)var2;
  140.                break;
  141.             case 8:
  142.                if (var2 == null || !(var2 instanceof Boolean)) {
  143.                   throw new SocketException("bad parameter for SO_KEEPALIVE");
  144.                }
  145.  
  146.                var3 = (Boolean)var2;
  147.                break;
  148.             case 15:
  149.                throw new SocketException("Cannot re-bind socket");
  150.             case 128:
  151.                if (var2 == null || !(var2 instanceof Integer) && !(var2 instanceof Boolean)) {
  152.                   throw new SocketException("Bad parameter for option");
  153.                }
  154.  
  155.                if (var2 instanceof Boolean) {
  156.                   var3 = false;
  157.                }
  158.                break;
  159.             case 4097:
  160.             case 4098:
  161.                if (var2 == null || !(var2 instanceof Integer) || (Integer)var2 <= 0) {
  162.                   throw new SocketException("bad parameter for SO_SNDBUF or SO_RCVBUF");
  163.                }
  164.                break;
  165.             case 4099:
  166.                if (var2 == null || !(var2 instanceof Boolean)) {
  167.                   throw new SocketException("bad parameter for SO_OOBINLINE");
  168.                }
  169.  
  170.                var3 = (Boolean)var2;
  171.                break;
  172.             case 4102:
  173.                if (var2 == null || !(var2 instanceof Integer)) {
  174.                   throw new SocketException("Bad parameter for SO_TIMEOUT");
  175.                }
  176.  
  177.                int var4 = (Integer)var2;
  178.                if (var4 < 0) {
  179.                   throw new IllegalArgumentException("timeout < 0");
  180.                }
  181.  
  182.                this.timeout = var4;
  183.                break;
  184.             default:
  185.                throw new SocketException("unrecognized TCP option: " + var1);
  186.          }
  187.  
  188.          this.socketSetOption(var1, var3, var2);
  189.       }
  190.    }
  191.  
  192.    public Object getOption(int var1) throws SocketException {
  193.       if (this.isClosedOrPending()) {
  194.          throw new SocketException("Socket Closed");
  195.       } else if (var1 == 4102) {
  196.          return new Integer(this.timeout);
  197.       } else {
  198.          int var2 = 0;
  199.          switch (var1) {
  200.             case 1:
  201.                var2 = this.socketGetOption(var1, (Object)null);
  202.                return var2 != -1;
  203.             case 3:
  204.                var2 = this.socketGetOption(var1, (Object)null);
  205.                if (var2 == -1) {
  206.                   return new Integer(this.trafficClass);
  207.                }
  208.  
  209.                return new Integer(var2);
  210.             case 4:
  211.                var2 = this.socketGetOption(var1, (Object)null);
  212.                return var2 != -1;
  213.             case 8:
  214.                var2 = this.socketGetOption(var1, (Object)null);
  215.                return var2 != -1;
  216.             case 15:
  217.                if (this.fd != null && this.fd1 != null) {
  218.                   return this.anyLocalBoundAddr;
  219.                }
  220.  
  221.                InetAddressContainer var3 = new InetAddressContainer();
  222.                this.socketGetOption(var1, var3);
  223.                return var3.addr;
  224.             case 128:
  225.                var2 = this.socketGetOption(var1, (Object)null);
  226.                return var2 == -1 ? Boolean.FALSE : new Integer(var2);
  227.             case 4097:
  228.             case 4098:
  229.                var2 = this.socketGetOption(var1, (Object)null);
  230.                return new Integer(var2);
  231.             case 4099:
  232.                var2 = this.socketGetOption(var1, (Object)null);
  233.                return var2 != -1;
  234.             default:
  235.                return null;
  236.          }
  237.       }
  238.    }
  239.  
  240.    private synchronized void doConnect(InetAddress var1, int var2, int var3) throws IOException {
  241.       try {
  242.          FileDescriptor var4 = this.acquireFD();
  243.  
  244.          try {
  245.             this.socketConnect(var1, var2, var3);
  246.             if (this.socket != null) {
  247.                this.socket.setBound();
  248.                this.socket.setConnected();
  249.             }
  250.          } finally {
  251.             this.releaseFD();
  252.          }
  253.  
  254.       } catch (IOException var9) {
  255.          this.close();
  256.          throw var9;
  257.       }
  258.    }
  259.  
  260.    protected synchronized void bind(InetAddress var1, int var2) throws IOException {
  261.       this.socketBind(var1, var2);
  262.       if (this.socket != null) {
  263.          this.socket.setBound();
  264.       }
  265.  
  266.       if (this.serverSocket != null) {
  267.          this.serverSocket.setBound();
  268.       }
  269.  
  270.       if (var1.isAnyLocalAddress()) {
  271.          this.anyLocalBoundAddr = var1;
  272.       }
  273.  
  274.    }
  275.  
  276.    protected synchronized void listen(int var1) throws IOException {
  277.       this.socketListen(var1);
  278.    }
  279.  
  280.    protected synchronized void accept(SocketImpl var1) throws IOException {
  281.       FileDescriptor var2 = this.acquireFD();
  282.  
  283.       try {
  284.          this.socketAccept(var1);
  285.       } finally {
  286.          this.releaseFD();
  287.       }
  288.  
  289.    }
  290.  
  291.    protected synchronized InputStream getInputStream() throws IOException {
  292.       if (this.isClosedOrPending()) {
  293.          throw new IOException("Socket Closed");
  294.       } else if (this.shut_rd) {
  295.          throw new IOException("Socket input is shutdown");
  296.       } else {
  297.          if (this.socketInputStream == null) {
  298.             this.socketInputStream = new SocketInputStream(this);
  299.          }
  300.  
  301.          return this.socketInputStream;
  302.       }
  303.    }
  304.  
  305.    void setInputStream(SocketInputStream var1) {
  306.       this.socketInputStream = var1;
  307.    }
  308.  
  309.    protected synchronized OutputStream getOutputStream() throws IOException {
  310.       if (this.isClosedOrPending()) {
  311.          throw new IOException("Socket Closed");
  312.       } else if (this.shut_wr) {
  313.          throw new IOException("Socket output is shutdown");
  314.       } else {
  315.          return new SocketOutputStream(this);
  316.       }
  317.    }
  318.  
  319.    protected synchronized int available() throws IOException {
  320.       if (this.isClosedOrPending()) {
  321.          throw new IOException("Stream closed.");
  322.       } else if (this.isConnectionReset()) {
  323.          return 0;
  324.       } else {
  325.          int var1 = 0;
  326.  
  327.          try {
  328.             var1 = this.socketAvailable();
  329.             if (var1 == 0 && this.isConnectionResetPending()) {
  330.                this.setConnectionReset();
  331.             }
  332.          } catch (ConnectionResetException var5) {
  333.             this.setConnectionResetPending();
  334.  
  335.             try {
  336.                var1 = this.socketAvailable();
  337.                if (var1 == 0) {
  338.                   this.setConnectionReset();
  339.                }
  340.             } catch (ConnectionResetException var4) {
  341.             }
  342.          }
  343.  
  344.          return var1;
  345.       }
  346.    }
  347.  
  348.    protected void close() throws IOException {
  349.       synchronized(this.fdLock) {
  350.          if (this.fd != null || this.fd1 != null) {
  351.             if (this.fdUseCount == 0) {
  352.                if (this.closePending) {
  353.                   return;
  354.                }
  355.  
  356.                this.closePending = true;
  357.  
  358.                try {
  359.                   this.socketPreClose();
  360.                } finally {
  361.                   this.socketClose();
  362.                }
  363.  
  364.                this.fd = null;
  365.                this.fd1 = null;
  366.                return;
  367.             }
  368.  
  369.             if (!this.closePending) {
  370.                this.closePending = true;
  371.                --this.fdUseCount;
  372.                this.socketPreClose();
  373.             }
  374.          }
  375.  
  376.       }
  377.    }
  378.  
  379.    protected void shutdownInput() throws IOException {
  380.       if (this.fd != null) {
  381.          this.socketShutdown(0);
  382.          if (this.socketInputStream != null) {
  383.             this.socketInputStream.setEOF(true);
  384.          }
  385.  
  386.          this.shut_rd = true;
  387.       }
  388.  
  389.    }
  390.  
  391.    protected void shutdownOutput() throws IOException {
  392.       if (this.fd != null) {
  393.          this.socketShutdown(1);
  394.          this.shut_wr = true;
  395.       }
  396.  
  397.    }
  398.  
  399.    protected boolean supportsUrgentData() {
  400.       return true;
  401.    }
  402.  
  403.    protected void sendUrgentData(int var1) throws IOException {
  404.       if (this.fd == null) {
  405.          throw new IOException("Socket Closed");
  406.       } else {
  407.          this.socketSendUrgentData(var1);
  408.       }
  409.    }
  410.  
  411.    protected void finalize() throws IOException {
  412.       this.close();
  413.    }
  414.  
  415.    public final FileDescriptor acquireFD() {
  416.       synchronized(this.fdLock) {
  417.          ++this.fdUseCount;
  418.          return this.fd;
  419.       }
  420.    }
  421.  
  422.    public final void releaseFD() {
  423.       synchronized(this.fdLock) {
  424.          --this.fdUseCount;
  425.          if (this.fdUseCount == -1 && this.fd != null) {
  426.             try {
  427.                this.socketClose();
  428.             } catch (IOException var8) {
  429.             } finally {
  430.                this.fd = null;
  431.             }
  432.          }
  433.  
  434.       }
  435.    }
  436.  
  437.    public boolean isConnectionReset() {
  438.       synchronized(this.resetLock) {
  439.          return this.resetState == this.CONNECTION_RESET;
  440.       }
  441.    }
  442.  
  443.    public boolean isConnectionResetPending() {
  444.       synchronized(this.resetLock) {
  445.          return this.resetState == this.CONNECTION_RESET_PENDING;
  446.       }
  447.    }
  448.  
  449.    public void setConnectionReset() {
  450.       synchronized(this.resetLock) {
  451.          this.resetState = this.CONNECTION_RESET;
  452.       }
  453.    }
  454.  
  455.    public void setConnectionResetPending() {
  456.       synchronized(this.resetLock) {
  457.          if (this.resetState == this.CONNECTION_NOT_RESET) {
  458.             this.resetState = this.CONNECTION_RESET_PENDING;
  459.          }
  460.  
  461.       }
  462.    }
  463.  
  464.    public boolean isClosedOrPending() {
  465.       synchronized(this.fdLock) {
  466.          return this.closePending || this.fd == null && this.fd1 == null;
  467.       }
  468.    }
  469.  
  470.    public int getTimeout() {
  471.       return this.timeout;
  472.    }
  473.  
  474.    private void socketPreClose() throws IOException {
  475.       this.socketClose0(true);
  476.    }
  477.  
  478.    private void socketClose() throws IOException {
  479.       this.socketClose0(false);
  480.    }
  481.  
  482.    private native void socketCreate(boolean var1) throws IOException;
  483.  
  484.    private native void socketConnect(InetAddress var1, int var2, int var3) throws IOException;
  485.  
  486.    private native void socketBind(InetAddress var1, int var2) throws IOException;
  487.  
  488.    private native void socketListen(int var1) throws IOException;
  489.  
  490.    private native void socketAccept(SocketImpl var1) throws IOException;
  491.  
  492.    private native int socketAvailable() throws IOException;
  493.  
  494.    private native void socketClose0(boolean var1) throws IOException;
  495.  
  496.    private native void socketShutdown(int var1) throws IOException;
  497.  
  498.    private static native void initProto();
  499.  
  500.    private native void socketSetOption(int var1, boolean var2, Object var3) throws SocketException;
  501.  
  502.    private native int socketGetOption(int var1, Object var2) throws SocketException;
  503.  
  504.    private native int socketGetOption1(int var1, Object var2, FileDescriptor var3) throws SocketException;
  505.  
  506.    private native void socketSendUrgentData(int var1) throws IOException;
  507.  
  508.    static {
  509.       AccessController.doPrivileged(new LoadLibraryAction("net"));
  510.       initProto();
  511.    }
  512. }
  513.